package org.ttitfly.ttcore.web.account;

import org.apache.commons.lang.StringUtils;
import org.springside.modules.orm.Page;
import org.ttitfly.ttcore.entity.account.Resource;
import org.ttitfly.ttcore.service.account.ResourceManager;
import org.ttitfly.ttcore.so.account.ResourceSO;
import org.ttitfly.ttcore.util.annotation.IsSearchObject;
import org.ttitfly.ttcore.util.annotation.PageSize;
import org.ttitfly.ttcore.web.CrudActionSupport;

/**
 * 用户管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数.
 * 演示带分页的管理界面.
 * 
 */

public class ResourceAction extends CrudActionSupport<Resource> {

	private static final long serialVersionUID = 8683878162525847072L;
	
	
	@PageSize(value = 3, exportLimit = 2000)
    private ResourcePageList pageList;

    @IsSearchObject
    private ResourceSO resourceSO;
	
    @javax.annotation.Resource
	private ResourceManager resourceManager;

	//-- 页面属性 --//
	private Long id;
	private Resource entity;
	private Page<Resource> page = new Page<Resource>(5);//每页5条记录
	

	//-- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public Resource getModel() {
		return entity;
	}

	public  void prepare() throws Exception {
		if (id != null) {
			entity = resourceManager.getResource(id);
		} else {
			entity = new Resource();
		}
	}
	
	public String listResource() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 新增资源
	 * @return
	 */
	public String preCreate() throws Exception{
		return SUCCESS;
	}
	
	public void  validateCreate() throws Exception {
		if(StringUtils.isNotBlank(entity.getTitle())){
			Resource resource = resourceManager.findResourceByTitle(entity.getTitle());
			if(resource != null){
				this.addFieldError("entity.title", this.getText("resource.title.duplicate"));
			}
		}
	}
	public String create() throws Exception{
		resourceManager.saveResource(entity);
		return SUCCESS;
	}
	
	
	public String preEdit() throws Exception{
		return SUCCESS;
	}

	public void  validateEdit() throws Exception {
		if(StringUtils.isNotBlank(entity.getTitle())){
			Resource resource = resourceManager.findResourceByTitle(entity.getTitle());
			if(resource != null){
				this.addFieldError("entity.title", this.getText("resource.title.duplicate"));
			}
		}
	}
	public String edit() throws Exception{
		resourceManager.saveResource(entity);
		return SUCCESS;
	}
	

	//-- 页面属性访问函数 --//
	/**
	 * list页面显示用户分页列表.
	 */
	public Page<Resource> getPage() {
		return page;
	}


	public ResourcePageList getPageList() {
		pageList = new ResourcePageList(resourceSO, resourceManager);
		return pageList;
	}

	public void setPageList(ResourcePageList pageList) {
		this.pageList = pageList;
	}

	public ResourceSO getResourceSO() {
		return resourceSO;
	}

	public void setResourceSO(ResourceSO resourceSO) {
		this.resourceSO = resourceSO;
	}

	public ResourceManager getResourceManager() {
		return resourceManager;
	}

	public void setResourceManager(ResourceManager resourceManager) {
		this.resourceManager = resourceManager;
	}


	public Resource getEntity() {
		return entity;
	}

	public void setEntity(Resource entity) {
		this.entity = entity;
	}

	@Override
	public String delete() throws Exception {
		resourceManager.deleteResource(id);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Long getId() {
		return id;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	
}
